package com.meter.config.controller;

import java.util.List;

import com.meter.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.meter.common.annotation.Log;
import com.meter.common.enums.BusinessType;
import com.meter.config.domain.ConfigAlarm;
import com.meter.config.service.IConfigAlarmService;
import com.meter.common.core.controller.BaseController;
import com.meter.common.core.domain.AjaxResult;
import com.meter.common.core.page.TableDataInfo;

/**
 * 告警配置Controller
 * 
 * @author ruoyi
 * @date 2025-05-15
 */
@Controller
@RequestMapping("/config/alarm")
public class ConfigAlarmController extends BaseController
{
    private String prefix = "config/alarm";

    @Autowired
    private IConfigAlarmService configAlarmService;

    @RequiresPermissions("config:alarm:view")
    @GetMapping()
    public String alarm()
    {
        return prefix + "/alarm";
    }

    /**
     * 查询告警配置列表
     */
    @RequiresPermissions("config:alarm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConfigAlarm configAlarm)
    {
        startPage();
        List<ConfigAlarm> list = configAlarmService.selectConfigAlarmList(configAlarm);
        return getDataTable(list);
    }

    /**
     * 新增告警配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存告警配置
     */
    @RequiresPermissions("config:alarm:add")
    @Log(title = "告警配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ConfigAlarm configAlarm)
    {
        configAlarm.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(configAlarmService.insertConfigAlarm(configAlarm));
    }

    /**
     * 修改告警配置
     */
    @RequiresPermissions("config:alarm:edit")
    @GetMapping("/edit/{alarmId}")
    public String edit(@PathVariable("alarmId") Long alarmId, ModelMap mmap)
    {
        ConfigAlarm configAlarm = configAlarmService.selectConfigAlarmByAlarmId(alarmId);
        mmap.put("configAlarm", configAlarm);
        return prefix + "/edit";
    }

    /**
     * 修改保存告警配置
     */
    @RequiresPermissions("config:alarm:edit")
    @Log(title = "告警配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConfigAlarm configAlarm)
    {
        configAlarm.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(configAlarmService.updateConfigAlarm(configAlarm));
    }

    /**
     * 删除告警配置
     */
    @RequiresPermissions("config:alarm:remove")
    @Log(title = "告警配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(configAlarmService.deleteConfigAlarmByAlarmIds(ids));
    }
}
